package com.iut2.projet2013;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ContactsList extends FragmentActivity {

	private MyCustomAdapter dataAdapter = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contact_list);

		// Generate list View from ArrayList
		displayListView();

		checkButtonClick();
	}

	private void displayListView() {

		// Array list of countries
		ArrayList<States> stateList = new ArrayList<States>();

		Cursor c;
		c = getContentResolver().query(
				ContactsContract.Contacts.CONTENT_URI,
				new String[] { ContactsContract.Contacts._ID,
						ContactsContract.Contacts.DISPLAY_NAME }, null, null,
				null);

		while (c.moveToNext()) {
			stateList.add(new States(c.getString(0), c.getString(1), false));
		}

		// create an ArrayAdaptar from the String Array
		dataAdapter = new MyCustomAdapter(this, R.layout.simple_list_item_1,
				stateList);
		ListView listView = (ListView) findViewById(R.id.listView);
		// Assign adapter to ListView
		listView.setAdapter(dataAdapter);
	}

	private class MyCustomAdapter extends ArrayAdapter<States> {

		private ArrayList<States> stateList;

		public MyCustomAdapter(Context context, int textViewResourceId,

		ArrayList<States> stateList) {
			super(context, textViewResourceId, stateList);
			this.stateList = new ArrayList<States>();
			this.stateList.addAll(stateList);
		}

		private class ViewHolder {
			TextView name;
			CheckBox checkbox;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder = null;

			if (convertView == null) {

				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

				convertView = vi.inflate(R.layout.simple_list_item_1, null);

				holder = new ViewHolder();
				holder.name = (TextView) convertView.findViewById(R.id.name);
				holder.checkbox = (CheckBox) convertView
						.findViewById(R.id.checkBox1);

				convertView.setTag(holder);

				holder.checkbox.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						CheckBox cb = (CheckBox) v;
						States _state = (States) cb.getTag();

						_state.setSelected(cb.isChecked());
					}
				});

			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			States state = stateList.get(position);

			holder.name.setText(state.getName());
			holder.checkbox.setChecked(state.isSelected());
			holder.checkbox.setTag(state);

			return convertView;
		}

	}

	private void checkButtonClick() {

		Button myButton = (Button) findViewById(R.id.addSelectedContacts);

		myButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				StringBuffer responseText = new StringBuffer();
				responseText.append("The following were selected...\n");

				ArrayList<States> stateList = dataAdapter.stateList;

				for (int i = 0; i < stateList.size(); i++) {
					States state = stateList.get(i);

					if (state.isSelected()) {
						responseText.append("\n" + state.getCode());
					}
				}

				Toast.makeText(getApplicationContext(), responseText,
						Toast.LENGTH_LONG).show();
			}
		});
	}
}
